﻿using System;

namespace CardGameCore.Battle
{
    public class SelectCardAi : ISelectCardAi
    {
        private readonly IBattlefield _battlefield;
        private readonly ISelectCardJudge _selectCardJudge;
        public SelectCardAi(IBattlefield battlefield, ISelectCardJudge selectCardJudge)
        {
            _battlefield = battlefield;
            _selectCardJudge = selectCardJudge;
        }

        public void Process(Actor actor)
        {
            Card temp = null;
            foreach (var card in actor.HandCards)
            {
                if (card.Cost + actor.BattleCost > _battlefield.MaxCost) continue;
                if (!_selectCardJudge.TryAddBattleCard(actor, card, out var position)) continue;

                temp = card;
                actor.BattleCards.Add(position, card);
                card.Position = position;
                actor.BattleCost += card.Cost;
                break;
            }

            if (temp != null)
            {
                actor.HandCards.Remove(temp);
            }
        }
    }
}